package forum.service.rest;

import com.foreveross.springboot.dubbo.utils.Payload;
import forum.domain.RoleResources;
import forum.domain.Token;
import forum.domain.UserRole;
import forum.service.rest.api.UserRoleRestService;
import forum.service.rpc.api.RoleResourcesService;
import forum.service.rpc.api.TokenService;
import forum.service.rpc.api.UserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/1.
 */
@Service("userRoleRestService")
@RestController
@RequestMapping(value = "/apis/userRole")
public class UserRoleRestServiceImpl implements UserRoleRestService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleResourcesService roleResourcesService;

    Map<String,String> error = new HashMap<String, String>();
    Map<String,Object> success = new HashMap<String, Object>();

    @RequestMapping(value = "/getListByUsername", method = RequestMethod.GET)
    public Payload getUserRoleListByUsername(@QueryParam("username") @DefaultValue("username=admin") String username,
                                             @QueryParam("token") @DefaultValue("null") String token) {


        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {
            //搞事情从这里开始
            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:user:getList") || list.getResources().getResourcesname().equals("user")) {
                            logger.info("你刚好有权限标志<------>apis:user:getList<------>||<------>user<------>");
                            //这里开始搞事情
                            success.put("content",userRoleService.getUserRoleListByUsername(username));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(userRoleService.getUserRoleListByUsername(username));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }

        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/user/getListByUsername访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/user/getListByUsername访问权限");
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Object getUserRoleList(@QueryParam("page") @DefaultValue("0") int page,
                                  @QueryParam("size") @DefaultValue("50") int size,
                                  @QueryParam("sort") @DefaultValue("sort=user_role_id,desc") String sort,
                                  @QueryParam("operation") @DefaultValue("operation=eq") String operation,
                                  @QueryParam("key") @DefaultValue("key=user_role_id") String key,
                                  @QueryParam("value") @DefaultValue("1") String value,
                                  @QueryParam("token") @DefaultValue("null") String token) {


        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:user:getList") || list.getResources().getResourcesname().equals("user")) {
                            logger.info("你刚好有权限标志<------>apis:user:getList<------>||<------>user<------>");
                            //这里开始搞事情
                            success.put("content",userRoleService.getUserRoleList(page,size,sort,operation,key,value));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(userRoleService.getUserRoleList(page,size,sort,operation,key,value));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }


        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/user/getListByUsername访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/user/getListByUsername访问权限");
    }



    @RequestMapping(value = "/updateByUsername", method = RequestMethod.PUT)//根据id修改
    @ResponseBody
    public Object updateUserRoleByUsername(@QueryParam("username") @DefaultValue("username=jack") String username,
                                           @QueryParam("token") @DefaultValue("null") String token,
                                           @RequestBody Map<String, Integer> jsonObj) {

        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {


            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:user:updateByUsername") || list.getResources().getResourcesname().equals("user")) {
                            logger.info("你刚好有权限标志<------>apis:user:updateByUsername<------>||<------>user<------>");
                            //这里开始搞事情
                            success.put("content",userRoleService.updateUserRoleByUsername(username,jsonObj));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(userRoleService.updateUserRoleByUsername(username,jsonObj));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }



        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/user/updateByUsername访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/user/updateByUsername访问权限");
    }

    @RequestMapping(value = "/createByUsername", method = RequestMethod.PUT)//根据id修改
    @ResponseBody
    public Object createUserRoleByUsername(@QueryParam("username") @DefaultValue("username=jack") String username,
                                           @QueryParam("token") @DefaultValue("null") String token,
                                           @RequestBody Map<String, Integer> jsonObj) {

        List<Token> tokenlist =new ArrayList<Token>();
        if (token==null){
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        }else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist!=null&&tokenlist.size()>0) {

            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>"+ roleList.get(0).getRole().getRolename().toString().trim() +"<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:user:createByUsername") || list.getResources().getResourcesname().equals("user")) {
                            logger.info("你刚好有权限标志<------>apis:user:createByUsername<------>||<------>user<------>");
                            //这里开始搞事情
                            success.put("content",userRoleService.createUserRoleByUsername(username,jsonObj));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(userRoleService.createUserRoleByUsername(username,jsonObj));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }



        }else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/user/createByUsername访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/user/createByUsername访问权限");
    }


    }
